<?php
namespace Tongcheng\Controller;
use Think\Controller;

/**
 * Class MemberController
 * @package Tongcheng\Controller
 * 用户后台管理
 */
class MemberController extends CommonController {

    /**
     * 用户管理列表
     */
    public function index(){
        $model = M("Member");
        $map = array();
//      $map['wid'] = session('wid');

        $page = $_GET['page']?$_GET['page']:1;
        $limit = $_GET['limit']?$_GET['limit']:10;
        $res['count'] = $model->where($map)->count();
        $list = $model->query('select *,FROM_UNIXTIME(m.create_time,"%m-%d %H:%i") addtime from tp_member m order by uid desc limit '.(intval($page)-1)*$limit.','.$limit);
        $res['list'] = $list;
        if(IS_AJAX){
            $this->ajaxReturn($res);
        }else{
            $this->assign('res', $res);
            $this->display();
        }
    }

    /**
     * 查看用户信息
     */
    public function edit(){
        $model = M("Member");
        $uid = I('uid');
        if(!empty($uid)){
            $list = $model->where('uid = '.$uid)->select();
            if(!empty($list[0])){
                $this->assign('list',$list[0]);
            }
        }
        $this->display();
    }

    /**
     * 用户余额充值
     */
    public function chongzhi(){
        $model = M("Member");
        $uid = I('uid');
        if(!empty($uid)){
            $list = $model->where('uid = '.$uid)->select();
            if(!empty($list[0])){
                $this->assign('list',$list[0]);
            }
        }
        $this->display();
    }

    /**
     * 执行充值操作
     */
    public function doChongZhi(){
        $chongzhinum = I('chongzhinum');
        $uid = I('uid');
        if(empty($uid)){
            $this->ajaxReturn(0);
        }
        $member = M('')->query('select * from tp_member m where m.uid = '.$uid);
        if(empty($member[0])){
            $this->ajaxReturn(0);
        }

        //增加一条订单记录，记录为后台充值
        $data['otheruid'] = $uid;
        $data['type'] = 3;
        $data['is_pay'] = 1;
        $data['uid'] = $uid;
        $data['wid'] = 2;
        $data['create_time'] = date("Y-m-d H:i:s");
        $data['last_time'] = date("Y-m-d H:i:s");
        $data['pay_time'] = date("Y-m-d H:i:s");
        $data['price'] = $chongzhinum;
        $data['pay_money'] = $chongzhinum;
        $data['shouru'] = $chongzhinum;
        $data['shuzhu_quren'] = 1;
        $data['title'] = "后台充值";
        $data['out_trade_no'] = 'HT'.$this->build_order_no();
        $data['otheruid'] = $uid;
        M('ShopOrder')->add($data);

        $result = M('Member')->where('uid = '.$uid)->save(array('yu_e_num'=>(floatval($member[0]['yu_e_num'])+floatval($chongzhinum))));
        if($result){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }

    /**
     * 明细
     */
    public function mingxi(){
        $model = M("YajinLog");
        $uid = $_GET['uid'];
        $balance = array();
        $balance['uid'] = $uid;
        $yajin = array();
        $yajin['uid'] = $uid;
        
        $page_balance = $_GET['page_balance']?$_GET['page_balance']:1;
        $limit_balance = $_GET['limit_balance']?$_GET['limit_balance']:10;
        $res['balance_count'] = M("YajinLog")->where($balance)->count();


        //查询余额
        $balance_list = $model->query('select * from tp_shop_order where uid= '.$uid.'  order by uid desc limit '.(intval($page_balance)-1)*$limit_balance.','.$limit_balance);
        $res['balance'] = $balance_list;
        $page_yajin = $_GET['page_yajin']?$_GET['page_yajin']:1;
        $limit_yajin = $_GET['limit_yajin']?$_GET['limit_yajin']:10;
        //查询押金记录
        $yajin_list = $model->query('select *,FROM_UNIXTIME(add_time,"%m-%d %H:%i") as addtime from tp_yajin_log where uid= '.$uid.'  order by uid desc limit '.(intval($page_yajin)-1)*$limit_yajin.','.$limit_yajin);
    
        $res['yajin_count'] = M("YajinLog")->where($yajin)->count();
        $res['yajin'] = $yajin_list;
        if(IS_AJAX){
            $this->ajaxReturn($res);
        }else{
            $this->assign('uid', $uid);
            $this->assign('res', $res);
            $this->display();
        }
    }

     public function detail(){
        $model = M("YajinLog");
        $uid = $_GET['uid'];
        $balance = array();
        $balance['uid'] = $uid;
        $yajin = array();
        $yajin['uid'] = $uid;
        
        $page_balance = $_GET['page_balance']?$_GET['page_balance']:1;
        $limit_balance = $_GET['limit_balance']?$_GET['limit_balance']:10;
        $res['balance_count'] = M("ShopOrder")->where($balance)->count();


        //查询余额
        $balance_list = M()->query('select * from tp_shop_order where uid= '.$uid.'  order by id desc limit '.(intval($page_balance)-1)*$limit_balance.','.$limit_balance);
        $res['balance'] = $balance_list;
        $page_yajin = $_GET['page_yajin']?$_GET['page_yajin']:1;
        $limit_yajin = $_GET['limit_yajin']?$_GET['limit_yajin']:10;
        //查询押金记录
        $yajin_list = $model->query('select *,FROM_UNIXTIME(add_time,"%m-%d %H:%i") as addtime from tp_yajin_log where uid= '.$uid.'  order by id desc limit '.(intval($page_yajin)-1)*$limit_yajin.','.$limit_yajin);
    
        $res['yajin_count'] = M("YajinLog")->where($yajin)->count();
        $res['yajin'] = $yajin_list;
        if(IS_AJAX){
            $this->ajaxReturn($res);
        }else{
            $this->assign('uid', $uid);
            $this->assign('res', $res);
            $this->display();
        }

     }
     protected function build_order_no(){
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }
}